import { getToken } from "./Auth";

export class WebSocketConnection {
    ws: WebSocket;

    constructor(env = 'dev') {
        let baseURL;
        if (env === 'dev') {
            baseURL = 'localhost:8036'
        } else {
            baseURL = '114.132.79.162:8036'
        }
        const token = getToken() || '';
        if (token === '') {
            window.location.href = `http://${env === 'dev' ? baseURL.replace('8036','5173'):baseURL.replace(':8036','')}/#/login`
            throw new Error('ws-token获取失败,自动跳转到登录界面');
        }
        const url = `ws://${baseURL}/ws/` + token;
        this.ws = new WebSocket(url);
    }
}